package com.xiaoxu.rental.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.rental.entity.AutoMaker;
import com.xiaoxu.rental.service.AutoMakerService;
import com.xiaoxu.rental.util.PinYinUtils;
import com.xiaoxu.rental.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author xiaoxu
 * @since 2024-07-24
 */
@Api(tags = "汽车品牌制造商控制器")
@RestController
@RequestMapping("/rental/autoMaker")
@SuppressWarnings("all")
public class AutoMakerController {
    @Resource
    private AutoMakerService autoMakerService;

    @ApiOperation(value = "分页查询汽车品牌制造商")
    @PostMapping("/{start}/{size}")
    public Result search(@PathVariable int start,
                         @PathVariable int size,
                         @RequestBody AutoMaker autoMaker) {
        Page<AutoMaker> page = autoMakerService.search(start, size, autoMaker);
        return Result.success().setData(page);
    }

    @ApiOperation(value = "删除汽车品牌制造商")
    @DeleteMapping("/{ids}") //1,2,3
    public Result delete(@PathVariable String ids) {
        List<Integer> list =
                Arrays.stream(ids.split(",")).map(Integer::parseInt).toList();
        return autoMakerService.removeByIds(list) ? Result.success() :
                Result.fail();
    }

    @ApiOperation(value = "保存汽车品牌制造商")
    @PostMapping("/save")
    public Result save(@RequestBody AutoMaker autoMaker) {
        autoMaker.setOrderLetter(PinYinUtils.getPinYin(autoMaker.getName()));
        return autoMakerService.save(autoMaker) ? Result.success() :
                Result.fail();
    }

    @ApiOperation(value = "更新汽车品牌制造商")
    @PutMapping("/update")
    public Result update(@RequestBody AutoMaker autoMaker) {
        autoMaker.setOrderLetter(PinYinUtils.getPinYin(autoMaker.getName()));
        autoMaker.setUpdateTime(LocalDateTime.now());
        return autoMakerService.updateById(autoMaker) ? Result.success() :
                Result.fail();
    }

    @ApiOperation(value = "查询所有汽车品牌制造商")
    @GetMapping("/getAll")
    public Result selectAll(){
        return Result.success().setData(autoMakerService.list());
    }



}
